/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import db.ConnectionFactory;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

/**
 *
 * @author hoang.nm
 */
public abstract class BaseController<T> {
    
    private Connection con = null;

    public BaseController() {
    }
    
    public void open() throws Exception {
        this.con = ConnectionFactory.createConnection();
    }
    
    public void close() throws Exception {
        this.con.close();
    }
    
    public ResultSet select(String cmd, String[] param) throws Exception {
        PreparedStatement pstmt = con.prepareStatement(cmd);
        if (param != null) {
            for (int i = 0; i < param.length; i++) {
                pstmt.setString(i+1, param[i]);
            }
        }
        return pstmt.executeQuery();
    }
    
    public int run(String cmd, String[] param) throws Exception {
        PreparedStatement pstmt = con.prepareStatement(cmd);
        if (param != null) {
            for (int i = 0; i < param.length; i++) {
                pstmt.setString(i+1, param[i]);
            }
        }
        return pstmt.executeUpdate();
    }
    
    public abstract List<T> getAll() throws Exception;
    public abstract T getById(T t) throws Exception;
    public abstract int add(T t) throws Exception;
    public abstract int update(T t) throws Exception;
    public abstract int delete(T t) throws Exception;
}
